package com.example.contact_recover.models;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

public class ContactsInfo extends LitePalSupport{

    private String name;
    private String number;
    private String sortKey;
    private int id;
    private User user ;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public ContactsInfo() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getSortKey() {
        return sortKey;
    }

    public void setSortKey(String sortKey) {
        this.sortKey = sortKey;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public static List<ContactsInfo> readContacts(Context context){


        List<ContactsInfo> contactsInfoList = new ArrayList<>();

        Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

        try {
            Cursor cursor = context.getContentResolver().query(contactUri,
                    new String[]{"display_name", "sort_key", "contact_id", "data1"},
                    null, null, "sort_key");
            String contactName;
            String contactNumber;
            String contactSortKey;
            int contactId;
            while (cursor.moveToNext()) {
                contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
               // contactSortKey = getSortkey(cursor.getString(1));
                contactSortKey = cursor.getString(1);
                ContactsInfo contactsInfo = new ContactsInfo(contactName, contactNumber, contactSortKey, contactId);
                if (contactName != null)
                   // System.out.println("add contact "+ contactsInfo);
                contactsInfoList.add(contactsInfo);
            }
            cursor.close();//使用完后一定要将cursor关闭，不然会造成内存泄露等问题




        } catch (   Exception e){
            System.out.println("exception "+ e.toString());
            e.printStackTrace();
        } finally

        {
            context = null;

        }
        return contactsInfoList;
    }


    public ContactsInfo(String name, String number, String sortKey, int id){
        setName(name);
        setNumber(number);
        setSortKey(sortKey);
        setId(id);

    }

    @Override
    public String toString() {
        return "ContactsInfo{" +
                "name='" + name + '\'' +
                ", number='" + number + '\'' +
                ", sortKey='" + sortKey + '\'' +
                ", id=" + id +
                '}';
    }
}
